// pages/route1/buy_suc/buy_suc.js
const app = getApp()
var timer = 1
Page({

  /**
   * 页面的初始数据
   */
  data: {
    session:'',
    productId:'',
    shareButton:'',
    getphone: false,
    mobile: '',
    getmsg: '获取验证码',
    sendmsg: 'sendmsg'
  },
  navtoposter:function(){
    var mobile = wx.getStorageSync('mobile')
    if (!mobile) {
      this.setData({
        getphone: true
      })
      return;
    }
wx.navigateTo({
  url: '../poster/poster?session=' + this.data.session + '&productId=' + this.data.productId,
})
  },
    navtodetails:function(){
    wx.navigateTo({
      url: "../poster1/poster1?orderId=" + this.data.orderId + "&ticketId=" + '&session=' + '&productId=' + this.data.productId,
    })
  },
    formSubmit: function (e) {
      var that = this;
      console.log(e)
      if ((e.detail.value.mobile == "") || (e.detail.value.verify == "")) {
        wx.showToast({
          title: '请完善信息',
          duration: 2000
        })
        return
      }
      else {
        var data = {
          "mobile": e.detail.value.mobile,
          "code": e.detail.value.verify,
          'session': wx.getStorageSync('session')
        }
        app.req("post", app.globalData.rootUrl + "/api/member/login/", data, function (res) {
          if (res.data.status == "200") {
            var session = wx.getStorageSync('session')
            wx.navigateTo({
              url: "../poster/poster?productId=" + that.data.productId + '&session=' + session
            })
          }
          else {
            wx.showToast({
              title: res.data.msg,
              duration: 2000
            })
          }
        })
      }
    },
    sendmessg: function (e) {
      var that = this
      if (timer == 1) {
        if (!(/^1[34578]\d{9}$/.test(this.data.mobile))) {
          wx.showToast({
            title: '手机号有误',
            duration: 2000
          })
          return
        }
        app.req("post", app.globalData.rootUrl + "/api/member/login/check", {
          "mobile": this.data.mobile,
          'session': wx.getStorageSync('session')
        }, function (res) {
          if (res.data.status == "200") {
            timer = 0
            var time = 60
            that.setData({
              sendmsg: "sendmsgafter",
            })
            var inter = setInterval(function () {
              that.setData({
                getmsg: time + "s",
              })
              time--
              if (time < 0) {
                timer = 1
                clearInterval(inter)
                that.setData({
                  sendmsg: "sendmsg",
                  getmsg: "获取验证码",
                })
              }
            }, 1000)
          }
          else {
            wx.showToast({
              title: res.data.msg,
              duration: 2000
            })
            return
          }
        })

      }
    },
    zzclose() {
      this.setData({
        getphone: false
      })
    },
    setmobile: function (e) {
      this.setData({
        mobile: e.detail.value
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      session: options.session,
      productId: options.productId,
      shareButton: wx.getStorageSync('shareButton2'),
      checkAfterJoin: wx.getStorageSync('checkAfterJoin'),
      orderId: options.orderId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getsession()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '我已加入活动，邀请您一起参加',
      path: 'pages/index/index?productId=' + this.data.productId + '&refId=' + wx.getStorageSync('memberId'),
      imageUrl: '../image/shareimg.jpg'
    }
  }
})